package user;

import java.sql.*;

public class DisplayRestaurantPage
{
	/*
	public static String getRestaurantList()
	{
		DBConnector db = DBConnector.getDBConnector();
		StringBuffer sBuf = new StringBuffer();	
		
		try {			
			db.prepareStatement("select rest_name, rest_link from restaurant");
			ResultSet rs = db.queryPstmt();			
			
			while (rs.next()){
				sBuf.append("<TR><TD align='center' width='50%'><div align='center'>" +
						"<A href='" + rs.getString(2) + ".jsp' " +
						"target=_blank class='16 16'>" +
						"<em><strong>" + rs.getString(1)+ "</strong></em></A></div></TD></TR>");
			}
			
			return sBuf.toString();
		}catch (SQLException ex){
			ex.printStackTrace();
			return "";
		}finally{
		}
	}
	*/
	
	//Retrieve the list of all the restaurant, display them in the dropdown menu in index.jsp
	public static String getRestaurantListItems()
	{
		DBConnector db = DBConnector.getDBConnector();
		StringBuffer sBuf = new StringBuffer();	
		
		try {			
			db.prepareStatement("select rest_name, rest_ID from restaurant");
			ResultSet rs = db.queryPstmt();
			
			if (rs != null)
			{
				while (rs.next()){
					sBuf.append("<option value='" + rs.getString(2) + "'>" + rs.getString(1) + "</option>");
				}
			}
			
			return sBuf.toString();
		}catch (SQLException ex){
			ex.printStackTrace();
			return "";
		}finally{			
		}
	}
	
	//Given the restaurant ID, retrieve restaurant name of the selected restaurant
	public static String getRestaurantName()
	{
		DBConnector db = DBConnector.getDBConnector();
		StringBuffer sBuf = new StringBuffer();	
		String Rest_ID = RestaurantQuery.getRestaurantSelected();
		
		try {			
			String statement = "select rest_name from restaurant where rest_ID = " + Rest_ID;
			db.prepareStatement(statement);
			ResultSet rs = db.queryPstmt();
			
			while (rs.next()){
				sBuf.append(rs.getString(1));
			}
			
			return sBuf.toString();
		}catch (SQLException ex){
			ex.printStackTrace();
			return "";
		}finally{		
		}
	}
	
	//Given the restaurant ID, retrieve restaurant address of the selected restaurant
	public static String getRestaurantAddress()
	{
		DBConnector db = DBConnector.getDBConnector();
		StringBuffer sBuf = new StringBuffer();	
		String Rest_ID = RestaurantQuery.getRestaurantSelected();
		
		try {	
			String statement = "select rest_address from restaurant where rest_ID = " + Rest_ID;
			db.prepareStatement(statement);
			ResultSet rs = db.queryPstmt();
			
			while (rs.next()){
				sBuf.append(rs.getString(1));
			}
			
			return sBuf.toString();
		}catch (SQLException ex){
			ex.printStackTrace();
			return "";
		}finally{			
		}
	}
	
	//Given the restaurant ID, retrieve restaurant phone number of the selected restaurant
	public static String getRestaurantPhoneNum()
	{
		DBConnector db = DBConnector.getDBConnector();
		StringBuffer sBuf = new StringBuffer();	
		String Rest_ID = RestaurantQuery.getRestaurantSelected();
		
		try {			
			String statement = "select rest_phone from restaurant where rest_ID = " + Rest_ID;
			db.prepareStatement(statement);
			ResultSet rs = db.queryPstmt();
			
			while (rs.next()){
				sBuf.append(rs.getString(1));
			}
			
			return sBuf.toString();
		}catch (SQLException ex){
			ex.printStackTrace();
			return "";
		}finally{
		}
	}	
	
	//Given the restaurant ID, retrieve restaurant open/close time of the selected restaurant
	public static String getRestaurantOpenCloseTime()
	{
		DBConnector db = DBConnector.getDBConnector();
		StringBuffer sBuf = new StringBuffer();	
		String Rest_ID = RestaurantQuery.getRestaurantSelected();
		
		try {			
			String statement = "select rest_open_time, rest_close_time from restaurant where rest_ID = " + Rest_ID;
			db.prepareStatement(statement);
			ResultSet rs = db.queryPstmt();
			
			while (rs.next()){
				sBuf.append(rs.getString(1) + " - " + rs.getString(2));
			}
			
			return sBuf.toString();
		}catch (SQLException ex){
			ex.printStackTrace();
			return "";
		}finally{
		}
	}
	
	//Given the restaurant ID, retrieve restaurant logo of the selected restaurant
	public static String getRestaurantLogo()
	{
		DBConnector db = DBConnector.getDBConnector();
		StringBuffer sBuf = new StringBuffer();	
		String Rest_ID = RestaurantQuery.getRestaurantSelected();
		
		try {			
			String statement = "select rest_pic_link from restaurant where rest_ID = " + Rest_ID;
			db.prepareStatement(statement);
			ResultSet rs = db.queryPstmt();
			
			while (rs.next()){
				sBuf.append("<IMG id='Mcdonalds_Logo' src='Pics/" + rs.getString(1) + "' width=150 height='150'>");
			}
			
			return sBuf.toString();
		}catch (SQLException ex){
			ex.printStackTrace();
			return "";
		}finally{
		}
	}
	
	//display the profit and loss of a selected restaurant, called in restaurant_account.jsp
	public static String getRestaurantProfitLoss(String RestID)
	{
		if (RestID != null)
    	{
			StringBuffer sBuf = new StringBuffer();	
			DBConnector db = DBConnector.getDBConnector();
			String queryRestaurantProfitCost = "select rest_revenue, rest_cost from restaurant where rest_ID = " + RestID;
	    	
			try{
				db.prepareStatement(queryRestaurantProfitCost);
		    	ResultSet queryRestaurantProfitCostResult = db.queryPstmt();
		    	
		    	if (queryRestaurantProfitCostResult != null && queryRestaurantProfitCostResult.next())
		    	{
					float Profit = queryRestaurantProfitCostResult.getFloat(1) - queryRestaurantProfitCostResult.getFloat(2);
					sBuf.append("<tr><td>Revenue : </td><td>" + queryRestaurantProfitCostResult.getString(1) + "</td></tr>");
					sBuf.append("<tr><td>Cost : </td><td>" + queryRestaurantProfitCostResult.getString(2) + "</td></tr>");
					sBuf.append("<tr><td>Profit : </td><td>" + Float.toString(Profit) + "</td></tr>");
		    	}
			}catch (SQLException ex){
				System.out.println("DisplayRestaurantPage.getRestaurantProfitLoss");
				return "";
			}finally{
			}
	    	return sBuf.toString();
    	}
		return "";
	}
	
	//get the restaurant name for the current logged in restaurant account, called in restaurant_account
	public static String getSelectedRestaurantName(String RestID)
	{
		DBConnector db = DBConnector.getDBConnector();
		StringBuffer sBuf = new StringBuffer();	
		String queryRestaurantName = "select rest_name from restaurant where rest_ID = '" + RestID + "'";
		
		try{
	    	db.prepareStatement(queryRestaurantName);
	    	ResultSet queryRestaurantNameResult = db.queryPstmt();    	
		
	    	if (queryRestaurantNameResult != null && queryRestaurantNameResult.next())
	    	{
	    		sBuf.append("<tr><td align = center>" + queryRestaurantNameResult.getString(1) + "</td></tr>");
	    	}
		}catch (SQLException ex){
			System.out.println("DisplayRestaurantPage.getSelectedRestaurantName");
			return "";
		}
    	return sBuf.toString();
	}
}
